<template>
    <el-date-picker v-model="value" type="daterange" range-separator="至" start-placeholder="开始日期"
        end-placeholder="结束日期" @change="dateChange">
    </el-date-picker>
</template>

<script>
    export default {
        data() {
            return {
                value: [],
                obj: {}
            }
        },
        props: {
            label: {
                type: Object,
                default: () => {}
            },
            comData: {
                type: Object,
                default: ()=>({})
            }
        },
        created() {
            if(this.comData.defaultTime && !this.comData.defaultValue) {
                let date = new Date()
                let year = date.getFullYear()
                let month = date.getMonth() + 1
                let day = date.getDate()
                month < 10 ? month = '0' + month : ''
                day < 10 ? day = '0' + day : ''
                this.value[0] = year + '-' + month + '-' + day + ' 00:00:00'
                this.value[1] = year + '-' + month + '-' + day + ' 23:59:59'
            }
            if(this.comData.defaultValue) {
                this.value[0] = this.comData.defaultValue[0]
                this.value[1] = this.comData.defaultValue[1]
            }
        },
        mounted() {
            if(this.label.label !== Number(this.label.label)) {
                this.obj[this.label.label[0]] = this.value[0]
                this.obj[this.label.label[1]] = this.value[1]
            } else {
                this.obj[this.label.label] = [this.value[0], this.value[1]]
                if(this.value.length == 0) {
                    this.obj[this.label.label] = []
                }
            }
            this.$emit('setFormData', this.obj)
        },
        methods: {
            defaultValueChange(d) {
                if(d.defaultValue && d.defaultValue.length > 0) {
                    this.value = d.defaultValue
                } else {
                    this.value = []
                }
            },
            dateChange() {
                if(this.value == null || !this.value.length) {
                    if(this.label.label !== Number(this.label.label)) {
                        this.obj[this.label.label[0]] = ''
                        this.obj[this.label.label[1]] = ''
                    } else {
                        this.obj[this.label.label] = []
                    }
                }else{
                    if(this.label.label !== Number(this.label.label)) {
                        this.obj[this.label.label[0]] = this.value[0].dateFormat(
                            "yyyy-MM-dd 00:00:00"
                        )
                        this.obj[this.label.label[1]] = this.value[1].dateFormat(
                            "yyyy-MM-dd 23:59:59"
                        )
                    } else {
                        this.obj[this.label.label] = [
                            this.value[0].dateFormat("yyyy-MM-dd 00:00:00"),
                            this.value[1].dateFormat("yyyy-MM-dd 23:59:59")
                        ]
                    }
                }
                this.$emit('setFormData', this.obj)
            }
        },
        watch: {
            comData: {
                handler(nVal, oVal) {
                    this.defaultValueChange(nVal)
                },
                deep: true
            },
        }
    }
</script>